package com.example.tms.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.tms.common.annotation.LogAction;
import com.example.tms.common.enums.Result;
import com.example.tms.entity.Department;
import com.example.tms.entity.EmployeeDepartment;
import com.example.tms.service.EmployeeDepartmentService;

@RestController
@RequestMapping("/employee-departments")
public class EmployeeDepartmentController {
    @Autowired
    private EmployeeDepartmentService employeeDepartmentService;

    @LogAction("获取所有员工-部门关联信息")
    @GetMapping("")
    @PreAuthorize("hasAnyRole('ADMIN','HR','EXECUTIVE')")
    public Result selectAll(Department department) {
        List<EmployeeDepartment> list = employeeDepartmentService.selectAll();
        return Result.success(list);
    }

    // GET: 根据 ID 获取员工-部门关联
    @LogAction("根据 ID 获取员工-部门关联")
    @GetMapping("/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result selectById(@PathVariable Integer id) {
        EmployeeDepartment employeeDepartment = employeeDepartmentService.selectById(id);
        return Result.success(employeeDepartment);
    }

    // POST: 添加员工-部门关系
    @LogAction("添加员工-部门关系")
    @PreAuthorize("hasAnyRole('ADMIN','HR','EXECUTIVE')")
    @PostMapping("")
    public Result add(@RequestBody EmployeeDepartment employeeDepartment) {
        int result = employeeDepartmentService.insert(employeeDepartment);
        return result > 0 ? Result.success() : Result.error("添加失败");
    }

    // PUT: 更新员工-部门信息
    @LogAction("更新员工-部门信息")
    @PreAuthorize("hasAnyRole('ADMIN','HR')")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody EmployeeDepartment employeeDepartment) {
        employeeDepartment.setId(id); // 将路径参数的 id 设置到 employeeDepartment 对象中
        int result = employeeDepartmentService.update(employeeDepartment);
        return result > 0 ? Result.success() : Result.error("更新失败");
    }

    // DELETE: 删除员工-部门关系
    @LogAction(value = "删除员工-部门关系", level = "WARN")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','HR')")
    public Result delete(@PathVariable Integer id) {
        int result = employeeDepartmentService.deleteById(id);
        return result > 0 ? Result.success() : Result.error("删除失败");
    }

    // 查询员工任职历程
    @LogAction("查询员工任职历程")
    @GetMapping("/histries/{empId}")
    @PreAuthorize("isAuthenticated()")
    public Result getEmployeeHistory(@PathVariable Integer empId) {
        List<EmployeeDepartment> list = employeeDepartmentService.selectByEmpId(empId);
        return Result.success(list);
    }

    // 查询部门员工列表
    @LogAction("查询部门员工列表") 
    @GetMapping("/departments/{depId}")
    @PreAuthorize("hasAnyRole('ADMIN','HR','EXECUTIVE')")
    public Result getDepartmentEmployees(@PathVariable Integer depId) {
        List<EmployeeDepartment> list = employeeDepartmentService.selectByDepId(depId);
        return Result.success(list);
    }

    // 获取员工当前部门信息
    @LogAction("获取员工当前部门信息")
    @GetMapping("/employees/{empId}")
    @PreAuthorize("isAuthenticated()")
    public Result getCurrentDepartmentInfo(@PathVariable Integer empId) {
        EmployeeDepartment department = employeeDepartmentService.getCurrentDepartment(empId);
        return Result.success(department);
    }

    // 获取所有待审批的人事调动
    @LogAction("获取所有待审批的人事调动")
    @GetMapping("/pending-transfers")
    @PreAuthorize("hasAnyRole('ADMIN','HR')")
    public Result getPendingTransfers() {
        List<EmployeeDepartment> transfers = employeeDepartmentService.getPendingTransfers();
        return Result.success(transfers);
    }

    // 获取指定创建人的所有人事调动申请
    @LogAction("获取创建人的人事调动申请")
    @PreAuthorize("hasAnyRole('ADMIN','EXECUTIVE')")
    public Result getTransfersByCreator(@PathVariable Integer creatorId) {
        List<EmployeeDepartment> transfers = employeeDepartmentService.getTransfersByCreator(creatorId);
        return Result.success(transfers);
    }
}
